package com.li.list;

import java.util.ArrayList;
import java.util.Stack;

public class 从尾到头打印链表 {
	public static void main(String[] args) {
		Node head = new Node();
		Node node1 = new Node(1);
		Node node2 = new Node(2);
		Node node3 = new Node(3);
		Node node4 = new Node(4);
		Node node5 = new Node(5);
		Node node6 = new Node(6);

		head.next = node1;
		node1.next = node2;
		node2.next = node3;
		node3.next = node4;
		node4.next = node5;
		node5.next = node6;
		ArrayList<Integer> list = printListFromTailToHead(head);
		for(Integer data : list){
			System.out.print(data+" ");
		}
	}
	
	public static ArrayList<Integer> printListFromTailToHead(Node node) {
        Stack<Integer> stack = new Stack<Integer>();
        while ((node = node.next) != null) {
            stack.push(node.value);
        }
 
        ArrayList<Integer> list = new ArrayList<Integer>();
        while (!stack.isEmpty()) {
            list.add(stack.pop());
        }
        return list;       
    }
}
